package mobi.hami.huapin.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.*;
import mobi.hami.huapin.base.Result;
import mobi.hami.huapin.model.Product;
import mobi.hami.huapin.model.Subject;
import mobi.hami.huapin.service.ManagementSubjectService;
import mobi.hami.huapin.util.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@ResponseBody
@RequestMapping(value = "/subject", produces = "application/json")
@Api(description = "供管理后台-商品主题管理-相关api")
public class ManagementSubjectController {

    private static Integer startPageNum = 1;
    private static Integer pageSize = 10;

    @Autowired
    private ManagementSubjectService managementSubjectService;


    /**
     * 主题管理-列表
     * @param mm
     * @return
     */
    @RequestMapping(value = "/list.do", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "主题管理-列表", notes = "{\n" +
            "  \"pageSize\": 每页容量,\n" +
            "  \"startPageNum\": 页码,\n" +
            "  \"name\": \"主题名称\",\n" +
            "  \"use_status\": \"主题状态 0表示未启用，1表示启用\",\n" +
            "  \"day_of_week\": \"周几，默认启用（周一为1，周日为7）\",\n" +
            "  \"description\": \"描述\",\n" +
            "  \"deleted\": \"删除状态，0表示正常，1表示已经删除\"\n" +
            "  \"start_gmt_create\": \"创建时间搜索区间开始\",\n" +
            "  \"end_gmt_create\": \"创建时间搜索区间结束\",\n" +
            "  \"start_gmt_modified\": \"修改时间搜索区间开始\",\n" +
            "  \"end_gmt_modified\": \"修改时间搜索区间开始\",\n" +
            "}")
    @ApiResponses({
            @ApiResponse(code = 200,message = "{\n" +
                    "  \"success\": true,\n" +
                    "  \"data\": {\n" +
                    "    \"startPageNum\": 1,\n" +
                    "    \"totalNum\": 10,\n" +
                    "    \"pageSize\": 10,\n" +
                    "    \"totalPageNum\": 1,\n" +
                    "    \"list\": [\n" +
                    "      {\n" +
                    "        \"id\": 1,\n" +
                    "        \"name\": \"主题名称\",\n" +
                    "        \"use_status\": 状态：0表示未启用，1表示启用,\n" +
                    "        \"day_of_week\": 周几，默认启用（周一为1，周日为7）,\n" +
                    "        \"description\": \"主题描述\",\n" +
                    "        \"deleted\": 删除状态，0表示正常，1表示已经删除,\n" +
                    "        \"gmt_create\": \"2018-08-27 06:54:26 创建时间\",\n" +
                    "        \"gmt_modified\": \"2018-08-27 14:56:50 修改时间\"\n" +
                    "      }\n" +
                    "    ]\n" +
                    "  }\n" +
                    "}")
    })
    public Result subject(@RequestBody Map<String,Object> mm){
        HashMap<String, Object> data = new HashMap<String, Object>();
        if (null != mm && null != mm.get("startPageNum")) {
            startPageNum = new Integer(String.valueOf(mm.get("startPageNum")));
        }
        if (null != mm && null != mm.get("pageSize")) {
            pageSize = new Integer(String.valueOf(mm.get("pageSize")));
        }
        Page page = PageHelper.startPage(startPageNum, pageSize, true).reasonable(true);
        List<Subject> list = managementSubjectService.selectUserInvoiceWitchParam(mm);
        PageInfo<Subject> pi = new PageInfo<>(list);
        int totalRecord = (int) page.getTotal();
        data.put("totalNum", totalRecord);
        int totalPageNum = (totalRecord + pageSize - 1) / pageSize;
        data.put("totalPageNum", totalPageNum);
        data.put("startPageNum", startPageNum);
        data.put("pageSize", pageSize);
        data.put("list", list);
        return Results.successWithData(data);
    }


    /**
     * 主题管理-刪除主题
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ApiOperation(value = "主题管理-刪除主题", notes = " ")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "id", value = "主题id", example = "1", paramType = "path", required = true, dataType = "int"),
            }
    )
    @ApiResponses({
            @ApiResponse(code = 200, message = "{\n" +
                    "  \"success\": true或者false,\n" +
                    "  \"status\": 0成功1失败,\n" +
                    "  \"msg\": \"提示消息\"\n" +
                    "}")
    })
    public Result delete(@PathVariable Integer id) {
        return managementSubjectService.delete(id);
    }


    /**
     * 主题管理-创建主题
     */
    @RequestMapping(value = "/add.do", method = RequestMethod.POST)
    @ApiOperation(value = "主题管理-创建主题", notes = "{\n" +
            "  \"name\": \"主题名称\",\n" +
            "  \"day_of_week\": 周几编码,\n" +
            "  \"description\": \"描述\"\n" +
            "}")
    @ApiResponses({
            @ApiResponse(code = 200, message = "{\n" +
                    "  \"success\": true或者false,\n" +
                    "  \"status\": 0成功1失败,\n" +
                    "  \"msg\": \"提示消息\"\n" +
                    "}")
    })
    public Result addSubject(@RequestBody Map<String,Object> mm){
        return managementSubjectService.addSubject(mm);
    }

    /**
     * 修改主题
     * @param sb
     * @return
     */
    @RequestMapping(value = "/updatesubject.do", method = RequestMethod.POST)
    @ApiOperation(value = "主题管理-修改主题", notes = "      {\n" +
            "        \"id\": 2,\n" +
            "        \"name\": \"早餐\",\n" +
            "        \"use_status\": 1,\n" +
            "        \"day_of_week\": 2,\n" +
            "        \"description\": \"测试\",\n" +
            "        \"deleted\": 1,\n" +
            "        \"gmt_create\": \"2018-08-27 06:54:26\",\n" +
            "        \"gmt_modified\": \"2018-10-15 14:22:36\"\n" +
            "      }")
    @ApiResponses({
            @ApiResponse(code = 200, message = "{\n" +
                    "  \"success\": true或者false,\n" +
                    "  \"status\": 0成功1失败,\n" +
                    "  \"msg\": \"提示消息\"\n" +
                    "}")
    })
    public Result updateSubject(@RequestBody Subject sb){
        return managementSubjectService.updateSubject(sb);
    }


    /**
     * 主题管理-主题下的-商品列表
     * @param mm
     * @return
     */
    @RequestMapping(value = "/subjectproducts.do", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "主题管理-主题下的-商品列表", notes = "{\n" +
            "\"startPageNum\":页码,\n" +
            "\"pageSize\":每页显示条数例如,100,\n" +
            "\"subject_id\":主题id,例如4,\n" +
            "\"name\":\"需要搜索的商品名称,例如,小,支持模糊搜索\",\n" +
            "\"product_code\":商品码,支持模糊搜索\n" +
            "\"deleted\":是否删除，1表示已经删除,即不在该主题下了，0表示正常,即该商品在该主题下\n" +
            "}")
    @ApiResponses({
            @ApiResponse(code = 200,message = "{\n" +
                    "  \"success\": true,\n" +
                    "  \"data\": {\n" +
                    "    \"startPageNum\": 当前页码,\n" +
                    "    \"totalNum\": 总记录数,\n" +
                    "    \"pageSize\": 每页容量,\n" +
                    "    \"totalPageNum\": 总页数,\n" +
                    "    \"list\": [\n" +
                    "      {\n" +
                    "        \"id\": 商品id,\n" +
                    "        \"name\": \"商品名称\",\n" +
                    "        \"product_code\": 商品码,\n" +
                    "        \"vodurl\": \"\",\n" +
                    "        \"subject_id\": null,\n" +
                    "        \"first_sortid\": null,\n" +
                    "        \"second_sortid\": 0,\n" +
                    "        \"third_sortid\": 0,\n" +
                    "        \"deleted\": null,\n" +
                    "        \"gmt_create\": null,\n" +
                    "        \"gmt_modified\": null,\n" +
                    "        \"order_num\": null\n" +
                    "      }\n" +
                    "    ]\n" +
                    "  }\n" +
                    "}")
    })
    public Result subject_products(@RequestBody Map<String,Object> mm){
        HashMap<String, Object> data = new HashMap<String, Object>();
        if (null != mm && null != mm.get("startPageNum")) {
            startPageNum = new Integer(String.valueOf(mm.get("startPageNum")));
        }
        if (null != mm && null != mm.get("pageSize")) {
            pageSize = new Integer(String.valueOf(mm.get("pageSize")));
        }
        Page page = PageHelper.startPage(startPageNum, pageSize, true).reasonable(true);
        List<Product> list = managementSubjectService.subject_products(mm);
        PageInfo<Product> pi = new PageInfo<>(list);
        int totalRecord = (int) page.getTotal();
        data.put("totalNum", totalRecord);
        int totalPageNum = (totalRecord + pageSize - 1) / pageSize;
        data.put("totalPageNum", totalPageNum);
        data.put("startPageNum", startPageNum);
        data.put("pageSize", pageSize);
        data.put("list", list);
        return Results.successWithData(data);
    }


    /**
     * 主题管理-把某个商品从某个主题下删除/上线
     * @param pid
     * @param subid
     * @return
     */
    @RequestMapping(value = "/updatesmdelete/{pid}/{subid}/{deleted}", method = RequestMethod.POST)
    @ApiOperation(value = "主题管理-把某个商品从某个主题下删除/上线", notes = "")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "pid", value = "商品id", example = "1", paramType = "path", required = true, dataType = "int"),
                    @ApiImplicitParam(name = "subid", value = "主题id", example = "1", paramType = "path", required = true, dataType = "int"),
                    @ApiImplicitParam(name = "deleted", value = "删除状态，0表示正常，1表示已经删除", example = "1", paramType = "path", required = true, dataType = "int"),
            }
    )
    @ApiResponses({
            @ApiResponse(code = 200, message = "{\n" +
                    "  \"success\": true或者false,\n" +
                    "  \"status\": 0成功1失败,\n" +
                    "  \"msg\": \"提示消息\"\n" +
                    "}")
    })
    public Result updatesmdelete(@PathVariable Integer pid,@PathVariable Integer subid,@PathVariable Integer deleted) {
        return managementSubjectService.updatesmdelete(pid,subid,deleted);
    }


}
